﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;

public partial class ServerList : System.Web.UI.Page
{
    public int sign = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            DAL.ServerDALs server = new DAL.ServerDALs();
            this.GridView1.DataSource = server.GetList();
            string[] keys ={ "Server_ID" };
            this.GridView1.DataKeyNames = keys;
            this.GridView1.DataBind();

            /**DAL.UserDALs userdals = new DAL.UserDALs();
            this.ddlUser.DataSource = userdals.GetList();
            this.ddlUser.DataBind();**/

        }
    } 
    
    protected void cmdAdd2_Click(object sender, EventArgs e)
    {
        DAL.ServerDALs server = new DAL.ServerDALs();
        server.Add(this.serverName.Text, this.serverIP.Text, this.DropDownList1.SelectedValue, this.TextBox1.Text,this.memo.Text);
        string[] keys ={ "Server_ID" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataSource = server.GetList();
        this.GridView1.DataBind();
        Response.Redirect("ServerList.aspx");
    }

    protected string DGFormatSex(string xb)
    {
        if (xb == "1")
            return "在用";
        else
            return "停用";
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DAL.ServerDALs server = new DAL.ServerDALs();
        string[] keys ={ "Server_ID" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataSource = server.GetList();
        this.GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        int id = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
        DAL.ServerDALs serverdals = new DAL.ServerDALs();
        Server server = new Server();
        server = serverdals.GetByID(id);

        this.serverName.Text = server.Name;
        this.server_id.Value = server.Server_id.ToString();
        this.serverIP.Text = server.Ip;

        string no = server.Status;
        this.DropDownList1.Items[this.DropDownList1.SelectedIndex].Selected = false;
        if (no == "1")
            this.DropDownList1.Items[0].Selected = true;
        else
            this.DropDownList1.Items[1].Selected = true;

        TextBox1.Text = server.Type;

        memo.Text = server.Comment;
        this.create_time.Value = server.Last_time.Value.ToLongTimeString();
        sign = 1;

    }


    protected void cmdAddupdate_Click(object sender, EventArgs e)
    {
        DAL.ServerDALs serverdals = new DAL.ServerDALs();
        Server server = new Server();
        server.Server_id = int.Parse(this.server_id.Value);
        server.Last_time = System.DateTime.Parse(this.create_time.Value);
        server.Name = this.serverName.Text;
        server.Type = this.TextBox1.Text;
        server.Ip = this.serverIP.Text;
        server.Status = this.DropDownList1.SelectedValue;
        serverdals.CreateUpdateDeleteServer(server, DAL.DataProviderAction.Update);
        Response.Redirect("ServerList.aspx");
        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        DAL.ServerDALs serverdals = new DAL.ServerDALs();
        serverdals.deleteServer(userid);
        string[] keys ={ "Server_ID" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataSource = serverdals.GetList();
        this.GridView1.DataBind();
        Response.Redirect("ServerList.aspx");
    }
}
